import re
import sys
import urllib.error
import urllib.parse
import urllib.request

from proxy_generator import ProxyGenerator

class CnproxyGenerator(ProxyGenerator):
  def __init__(self):
    super().__init__()
    self.pages = ["proxy%d.html" % i for i in range(1, 11)]
    self.pages.extend(["proxyedu1.html", "proxyedu2.html"])
    self.pages = ["http://www.cnproxy.com/" + p for p in self.pages]

    self._ip_port_pattern = re.compile(
        '<tr><td>(\d*.\d*.\d*.\d*)' +
        '<SCRIPT type=text\/javascript>' +
        'document.write\(":"([^)]*)\)<\/SCRIPT><\/td><td>(.*?)<\/td>')
    self._port_mapping_pattern = re.compile(
        '<SCRIPT type="text\/javascript">([^<]+)<\/SCRIPT>')
    self._mapping_item_pattern = re.compile('([a-z])="([0-9]+)";')

  def GetIps(self, webdata):
    webdata = webdata.decode('GBK')
    ip_ports = self._ip_port_pattern.findall(webdata)
    port_mapping = self._GetPortMap(webdata)
    # print('port map: ' + str(port_mapping))
  
    for pair in ip_ports:
      proxy = pair[0] + ':' + self._GetPort(port_mapping, pair[1])
      yield proxy

  def _GetPortMap(self, webdata):
    port_mapping = self._port_mapping_pattern.findall(webdata)
    mapping_items = self._mapping_item_pattern.findall(port_mapping[0])
    return {m[0] : m[1] for m in mapping_items}
  
  def _GetPort(self, port_mapping, key):
    key = key.replace("+", "")
    port = ""
    for c in list(key):
      port += port_mapping[c]
    return port
